using Mark.Handlers;
using Microsoft.EntityFrameworkCore;
using Mark.Data;
using Mark.Services;
using Mark.Services.Interfaces;

#if DEBUG
using Microsoft.Extensions.Logging;
#endif

namespace Mark;

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
            })
            .ConfigureMauiHandlers(handlers =>
            {
                // 注册自定义WebView处理器以支持本地文件请求拦截
                handlers.AddHandler<WebView, CustomWebViewHandler>();
#if DEBUG
                System.Diagnostics.Debug.WriteLine("自定义WebView处理器已注册");
#endif
            });

        // 配置数据库
        builder.Services.AddDbContext<MarkDbContext>(options =>
        {
            var dbPath = Path.Combine(FileSystem.AppDataDirectory, "mark.db");
            options.UseSqlite($"Data Source={dbPath}");
#if DEBUG
            options.EnableSensitiveDataLogging();
            options.LogTo(message => System.Diagnostics.Debug.WriteLine(message));
#endif
        });

        // 注册服务
        builder.Services.AddScoped<IChatService, ChatService>();
        builder.Services.AddScoped<WebViewBridge>();
        builder.Services.AddScoped<DatabaseInitializer>();
        
        // 注册页面
        builder.Services.AddSingleton<MainPage>();
        
#if DEBUG
        builder.Logging.AddDebug();
#endif

        return builder.Build();
    }
} 